﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Text;

namespace WisdriContacts
{
    public partial class ActivePage : PhoneApplicationPage
    {
        public ActivePage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string dateSeed = DateTime.Now.ToString("yyyyMM");

            //建立SHA1对象
            SHA1 sha = new SHA1Managed();

            //将mystr转换成byte[]
            byte[] dataToHash = UTF8Encoding.UTF8.GetBytes(dateSeed);

            //Hash运算
            byte[] dataHashed = sha.ComputeHash(dataToHash);

            //将运算结果转换成string
            string hash = BitConverter.ToString(dataHashed).Replace("-", "");

            var key = hash.Substring(1, 5);


            if (TextBoxKey.Text.Trim() == key)
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                settings["IsActived"] = true;
                settings["IsAdmin"] = false;
                settings.Save();
                MessageBox.Show("激活成功");
                App.ViewModel.LoadData();
                NavigationService.Navigate(new Uri("/DepartmentPage.xaml", UriKind.Relative));
            }
            else if (TextBoxKey.Text.Trim() == "liqi")
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                settings["IsActived"] = true;
                settings["IsAdmin"] = true;
                settings.Save();
                MessageBox.Show("激活管理员模式成功");
                App.ViewModel.LoadData();
                NavigationService.Navigate(new Uri("/DepartmentPage.xaml", UriKind.Relative));
            }
            else
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                settings["IsActived"] = false;
                settings["IsAdmin"] = false;
                settings.Save();
                MessageBox.Show("激活失败");
            }
        }
    }
}